package grape.wx.service.mp.user.api;

import grape.common.service.trans.ITransService;
import grape.wx.service.mp.MpProperties;
import grape.wx.service.mp.user.po.MpUser;
import grape.common.service.common.IBaseService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
 * <p>
 * 公众号微信用户表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-07-27
 */
public interface IMpUserService extends IBaseService<MpUser> , ITransService<String, String> {

    public static final String trans_type_userNickname ="userNickname";
    public static final String trans_type_userAvatar ="userAvatar";
    /**
     * 根据openId和appCode判断是否存在
     * @param openId
     * @param appCode
     * @return
     */
    default boolean existByOpenidAndAppCode(String openId,String appCode) {
        return getByOpenidAndAppCode(openId,appCode) != null;
    }

    /**
     * 根据openId和appCode获取
     * @param openId
     * @param appCode
     * @return
     */
    default MpUser getByOpenidAndAppCode(String openId,String appCode) {
        assertParamNotEmpty(openId,"openId不能为空");
        assertParamNotEmpty(appCode,"appCode不能为空");
        MpUser mpUser = new MpUser();
        mpUser.setAppCode(appCode);
        mpUser.setOpenId(openId);
        return getOne(mpUser);
    }

    /**
     * 添加用户
     * @param wxMpUser
     * @param mpConfig
     * @return
     */
    MpUser insert(WxMpUser wxMpUser, MpProperties.MpConfig mpConfig, MpUser.DictHowFrom dictHowFrom);
    /**
     * 添加用户
     * @param wxMpUser
     * @param mpConfig
     * @param mpUser
     * @return
     */
    MpUser updateById(WxMpUser wxMpUser, MpProperties.MpConfig mpConfig, MpUser mpUser);


    @Override
    default boolean support(String type){
        return isEqualAny(type, trans_type_userNickname,trans_type_userAvatar);
    }

    @Override
    default String trans(String type, String key){
        MpUser user = getById(key);
        if (user != null) {
            if(isEqual(type,trans_type_userNickname)){
                return user.getNickname();
            }  else  if(isEqual(type,trans_type_userAvatar)){
                return user.getAvatar();
            }
        }
        return null;
    }
}
